﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Gqq.Common.Helper
{
    public static class ButtonHelper
    {
        /// <summary>
        /// 扩展Html(一定要记住一个命名空间，System.Web.Mvc，来自System.Web.Mvc.dll
        /// </summary>
        /// <param name="id"></param>
        /// <param name="html"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="onClick"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        //MvcHtmlString需要引用System.Web.dll
        public static MvcHtmlString Button(this HtmlHelper html, string name, string value, string id = null, string onClick = null, IDictionary<string, object> htmlAttributes = null)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            //TagBuilder在System.Web.WebPages中(System.Web.WebPages.dll，包含命名空间为System.Web.Helpers、System.Web.Mvc、System.Web.WebPages、System.Web.WebPages.Html、System.Web.WebPages.Scope)
            TagBuilder buttonBuilder = new TagBuilder("input");
            buttonBuilder.MergeAttribute("name", name);
            buttonBuilder.MergeAttribute("value", value);
            buttonBuilder.MergeAttribute("type", "button");
            if (string.IsNullOrEmpty(id))
                id = name;
            buttonBuilder.MergeAttribute("id", id);

            if (!string.IsNullOrEmpty(onClick))
                buttonBuilder.MergeAttribute("onclick", onClick);
            buttonBuilder.MergeAttributes(htmlAttributes, true);
            return MvcHtmlString.Create(buttonBuilder.ToString(TagRenderMode.SelfClosing));
        }
    }
}
